import {Injectable} from '@angular/core';
import {Menu} from '../entity/menu';
import {Observable} from 'rxjs/index';
import {DocApi} from '../entity/doc-api';
import {DocService} from './doc.service';

@Injectable({
    providedIn: 'root'
})
export class DocSearchService {

    constructor(private docService: DocService) {
    }

    search(keyword: string): Observable<Menu[]> {
        const doc = this.docService.getCurrentDoc();
        const menus: Menu[] = [];

        if (!doc || !doc.chapters) {
            return new Observable<Menu[]>(observer => observer.next(menus));
        }
        doc.chapters.forEach(chapter => {
            // 可以对标题做筛选
            if (chapter.sections) {
                // 可以对section标题做筛选
                let chapterMenu = null;
                chapter.sections.forEach(section => {
                    if (section.apis) {
                        let sectionMenu = null;
                        section.apis.forEach(api => {
                            const menu = this.searchApi(api, keyword);
                            if (menu) {
                                if (!chapterMenu) {
                                    chapterMenu = <Menu> {
                                        name: chapter.name,
                                        expanding: true,
                                        children: []
                                    };
                                    menus.push(chapterMenu);
                                }
                                if (!sectionMenu) {
                                    sectionMenu = <Menu> {
                                        name: section.name,
                                        expanding: true,
                                        children: []
                                    };
                                    chapterMenu.children.push(sectionMenu);
                                }
                                sectionMenu.children.push(menu);
                            }
                        });
                    }
                });
            }
        });
        return new Observable<Menu[]>(observer => observer.next(menus));
    }

    private searchApi(api: DocApi, keyword: string): Menu {
        let index = api.name.indexOf(keyword);
        let menu = null;
        if (index !== -1) {
            menu = <Menu>{
                name: api.name,
                url: ['doc', this.docService.getCurrentDoc().artifact.id, api.key]
            };
        }

        if (api.paths) {
            api.paths.forEach(path => {
                index = path.indexOf(keyword);
                if (index !== -1) {
                    menu = <Menu>{
                        name: api.name,
                        url: ['doc', this.docService.getCurrentDoc().artifact.id, api.key]
                    };
                }
            });
        }

        if (api.code) {
            index = api.code.indexOf(keyword);
            if (index !== -1) {
                menu = <Menu>{
                    name: api.name,
                    url: ['doc', this.docService.getCurrentDoc().artifact.id, api.key]
                };
            }
        }
        return menu;
    }
}
